﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GnufItem.h"

namespace gnuf {

    class Block;
    class TabBar;
    class Button;
    class QtToolbar;
    class WorkspaceTabBar;
    class TreeBoard;
    class NameValuePairs;
    class FamilyTypes;
    class Label;
    class ButtonEx;
    class LineEditComboBox;

    class GNUF_BASE_EXPORT Toolbar final 
        : public ItemWith<QtToolbar, RootItem>
    {
        CANNOT_COPY_OR_ASSIGN(Toolbar)
    public:
        static const EItemType Type;
        explicit Toolbar(const gcmp::UniqueString * uniqueKey);
        virtual ~Toolbar();

        void wantBreadcrumb();
        const Label * breadcrumb() const;
              Label * breadcrumb();

        template<typename TItem>
        TItem* addItem()
        {
            static_assert(std::is_base_of<Item, TItem>::value, "typename just only inherit from Item");
            auto nvp = NEW_AS_OWNER_PTR(TItem, this);
            auto ptr = nvp.get();
            m_contents.push_back(TransferOwnership(nvp));

            return ptr;
        }

        template<typename TItem>
        TItem* insertItem(int index)
        {
            static_assert(std::is_base_of<Item, TItem>::value, "typename just only inherit from Item");
            gcmp::OwnerPtr<TItem> item = NEW_AS_OWNER_PTR(TItem, this);
            TItem* ptr = item.get();

            // maybe should display a debug warning rather than default behavior
            if (index < 0 || index >= m_contents.size())
            {
                m_contents.push_back(TransferOwnership(item));
            }
            else
            {
                auto itor = m_contents.begin();
                advance(itor, index);
                m_contents.insert(itor, TransferOwnership(item));
            }
            return ptr;
        }

        typedef std::list< gcmp::OwnerPtr<Item> > Contents;
        typedef std::vector< Item * > RawContents;
        inline const Contents & contents() const {
            return m_contents;
        }
        RawContents rawContents();

        Item* itemAt(int index);
        bool dumpItem(Item* pItem);
        bool removeItem(Item* pItem);

        int indexOf(const Item *) const;
        void clearContents();

        virtual void transferContentsTo(Item *,
            const Item::DecideTranferringContentMethod &,
            const Item::PostContentTransferred &) override;

        void setWantBlockCaption(bool);
        bool wantBlockCaption() const { return ((m_blockCaptionVisibility & 0xf0) > 0); }
        void setBlockCaptionVisible(bool);
        bool isBlockCaptionVisible()  const { return ((m_blockCaptionVisibility & 0x0f) > 0); }
    protected:
        // Inherits from ItemWith...
        virtual void onDisconnectFromMyQtItem() override;
        virtual void onPostAppearanceSettingChanged() override;
        virtual void onChildHeightChanged(Item *, int) override;
        virtual void onPostScaleFactorChanged() override;
        virtual QtItem * getQtItem() const override;
        virtual bool onAllowToTransferChild(
            const Item * pChild, const Item * pNewParent) const override;

        Item * itemBy(EItemType);

    private:
        bool transferInAdvance(Block *, Toolbar *);
        gcmp::OwnerPtr< Label > m_breadcrumb;
        Toolbar::Contents m_contents;
        bool m_transferingChild = false;
        std::uint8_t m_blockCaptionVisibility = 0;
    };
}